﻿String.prototype.ToDate = function () {
    var date = new Date(parseInt(this.replace("/Date(", "").replace(")/", ""), 10));
    return date;
}
Date.prototype.Format = function (format) {
    /*
    * eg:format="YYYY-MM-dd hh:mm:ss";
    */
    var o = {
        "M+": this.getMonth() + 1,  //month
        "d+": this.getDate(),     //day
        "h+": this.getHours(),    //hour
        "m+": this.getMinutes(),  //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

String.prototype.endWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substring(this.length - str.length) == str)
        return true;
    else
        return false;
    return true;
}


function showAttendanceStatus(time, timeType, dayType, lmax, isWeekend, minTime){
    var statusName = "";//正常
    if(isWeekend){
        statusName="休日出勤";
        return statusName;
    }
    if(lmax != null && (lmax==4|| lmax==5)){
        statusName="正常（需核实前一天延时）";
        return statusName;
    }

    var timeArray = time.split(':');
    switch(timeType){
        case 1://上班
            if(dayType==1) { //固定
                if (timeArray[0] > 9 || (timeArray[0] == 9 && timeArray[1] > 5)) {
                    if (lmax != null && lmax >= 22) {
                        if (timeArray[0] < 10 || (timeArray[0] == 10 && timeArray[1] == 0)) {
                            statusName = "正常（需核实前一天延时）";
                        }
                        else if (timeArray[0] == 10 && timeArray[1] <= 30) {
                            statusName = "迟到（需核实前一天延时）";
                        }
                        else {
                            statusName = "旷工";
                        }
                    }
                    else if (lmax != null && lmax >= 0 && lmax <= 3) {
                        if (timeArray[0] < 12 || (timeArray[0] == 13 && timeArray[1] == 0)) {
                            statusName = "正常（需核实前一天延时）";
                        }
                        else if (timeArray[0] == 13 && timeArray[1] <= 30) {
                            statusName = "迟到（需核实前一天延时）";
                        }
                        else {
                            statusName = "旷工";
                        }
                    }
                    else {
                        if (timeArray[0] > 9 || (timeArray[0] == 9 && timeArray[1] > 30)) {
                            statusName = "旷工";
                        }
                        else if (timeArray[0] == 9 && timeArray[1] > 5) {
                            statusName = "迟到";
                        }
                    }
                }
            }
            else if(dayType==2){
                if (timeArray[0] > 9 || (timeArray[0] == 9 && timeArray[1] > 30)) {
                    if (lmax != null && lmax >= 22) {
                        if (timeArray[0] < 10 || (timeArray[0] == 10 && timeArray[1] == 0)) {
                            statusName = "正常（需核实前一天延时）";
                        }
                        else if (timeArray[0] == 10 && timeArray[1] <= 30) {
                            statusName = "迟到（需核实前一天延时）";
                        }
                        else {
                            statusName = "旷工";
                        }
                    }
                    else if (lmax != null && lmax >= 0 && lmax <= 3) {
                        if (timeArray[0] < 12 || (timeArray[0] == 13 && timeArray[1] == 0)) {
                            statusName = "正常（需核实前一天延时）";
                        }
                        else if (timeArray[0] == 13 && timeArray[1] <= 30) {
                            statusName = "迟到（需核实前一天延时）";
                        }
                        else {
                            statusName = "旷工";
                        }
                    }
                    else {
                        if (timeArray[0] > 9) {
                            statusName = "旷工";
                        }
                        else if (timeArray[0] == 9 && timeArray[1] > 30) {
                            statusName = "迟到";
                        }
                    }
                }
            }
            break;
        case 2://下班
            if(dayType == 1) { //固定
                if (timeArray[0] < 18 && timeArray[0] > 5) {
                    if(timeArray[0] < 17 || (timeArray[0] == 17 && timeArray[1] < 30)){
                        statusName="旷工";
                    }
                    else{
                        statusName="早退";
                    }
                }
                else if(timeArray[0] >= 22 || timeArray[0]<= 5){
                    statusName = "延时";
                }
            }
            else if (dayType == 2) {
                if ((timeArray[0] >5 && timeArray[0] <18) || (timeArray[0] == 18 && timeArray[1] < 30)) {
                    if (lmax != null && (lmax >= 22 || (lmax >= 0 && lmax <= 3))) {
                        if (timeArray[0] < 17 || (timeArray[0] == 17 && timeArray[1] == 30)) {
                            statusName = "旷工";
                        }
                        else if (timeArray[0] < 18) {
                            statusName = "早退";
                        }
                    }
                    else {
                        var minTimeArray = minTime.split(':');
                        if(minTimeArray[0]>=10 || (minTimeArray[0] ==9 && minTimeArray[1]>30)){
                            if (timeArray[0] < 18) {
                                statusName = "旷工";
                            }
                            else{
                                statusName = "早退";
                            }
                        } else {
                            var date1 = new Date("2012/01/01 " + minTime);
                            var date2 = new Date("2012/01/01 " + time);
                            if (date2 - date1 < 30600000) {
                                statusName = "旷工";
                            } else if (date2 - date1 < 32400000) {
                                statusName = "早退";
                            }
                        }
                    }
                }
                else if(timeArray[0]>=22||timeArray[0]<=5){
                    statusName="延时";
                }
            }
            break;
        default:
            break;
    }

    return statusName;
}

function showSchedule(date, specialSchedule, schedule) {
    var date = new Date(date);

    var special = null;
    $.each(specialSchedule, function (i, q) {
        if (q.FromDate.ToDate() <= date && q.EndDate.ToDate() >= date) {
            special = q;
            return false;
        }
    });

    if (special != null)
        return { type: special.Type, name: special.TypeName };

    if (schedule == null)
        return { type: 1, name: "固定" };

    return { type: schedule.Type, name: schedule.TypeName };
}

function getAngelName(id) {
    var list = page && page.angelList ? page.angelList : [];
    if(id==null)return "";
    var name = id;
    if(id!=null){
        $.each(list, function (i, q) {
            if(q.Id==id.toLowerCase()){
                name= q.UserName;
                return false;
            }
        });
    }
    return id==name? getUserName(id): name +"("+id+")";
}

function getUserName(id) {
    if (id == null) return "";

    var list = page && page.allUser ? page.allUser : [];
    var name = id;
    if(id!=null){
        $.each(list, function (i, q) {
            if(q.id == id.toLowerCase()){
                name= q.username;
                return false;
            }
        });
    }
    return name;
}

var global_type_list = ['加班延时', '异常', '事假', '病假', '丧假', '产假', '法定年假', '福利年假', '调休', '调休加班', '周末值班', '公假值班', '其他'];
function showTypeName(type) {
    if (type > global_type_list.length)
        return "未知";
    return global_type_list[type];
}

function showTypeSelect(type){
    var html = [];
    html.push("<select id='cra_type'>");
    $.each(global_type_list, function (i, q) {
        html.push("<option value='"+i+"'"+ (i==type?" selected='selected'":"")+">"+q+"</option>");
    });
    return html.join('');
}

function showTimeTypeName(duration) {
    if (duration == 0) return " - ";
    return (duration >= 8 ? (parseInt(duration / 8, 10) + " 天 ") : "") + (duration % 8 == 0 ? "" : ((duration % 8) + " 小时"));
}